document.getElementById("favorite_name").addEventListener("blur", function () {
    //todo
    var reg=/^[.]{1,6}$/;
    if (!reg.test(this.value)){
        //alert("链接名称长度有错误");
    }

})
//1. 当页面加载完成后，发送ajax请求
window.onload = function () {
    //2. 发送ajax请求
    axios({
        method:"get",
        url:"getMyFavoriteCategorys"
    }).then(function (resp) {
        let list = resp.data;
        let favoriteCategorys="收藏分类：";
        for (let i = 0; i < list.length ; i++) {
            let obj = list[i];
            //<input type="radio" name="departmentId" value="${department.id}" >${department.name}
            favoriteCategorys += "<input type=\"radio\" name=\"favorite_category_id\" value="+
                obj.id +" >"+obj.categoryName;
        }
        // 设置表格数据
        document.getElementById("my_link_category").innerHTML = favoriteCategorys;
    })
}
//1. 给按钮绑定单击事件
document.getElementById("btn").onclick = function () {
    // 将表单数据转为json
    var formData = {
        favoriteName:"",
        linkUrl:"",
        categoryId:"",
        loginName:"",
        password:"",
    };
    // 获取表单数据
    formData.favoriteName = document.getElementById("favorite_name").value;
    // 获取表单数据
    formData.linkUrl= document.getElementById("link_url").value;
    // 获取表单数据
    formData.loginName= document.getElementById("login_name").value;
    // 获取表单数据
    formData.password= document.getElementById("password").value;

    let favoriteCategorys = document.getElementsByName("favorite_category_id");
    for (let i = 0; i < favoriteCategorys.length; i++) {
        if(favoriteCategorys[i].checked){
            //获取选中的链接分类的id
            formData.categoryId = favoriteCategorys[i].value ;
        }
    }
    console.log(formData);
    //2. 发送ajax请求
    axios({
        method:"post",
        url:"addFavorite",
        data:formData
    }).then(function (resp) {
        // 判断响应数据是否为 success
        if(resp.data == "success"){
            location.href = "my_favorite_list.html";
        }else {
            alert("收藏失败！")
        }
    })
}